package com.lianzai.car.service.impl;

import com.lianzai.car.dao.CarInfoDao;
import com.lianzai.car.dao.CarLogDao;
import com.lianzai.car.domain.CarLogDO;
import com.lianzai.car.domain.CarParkInfoDO;
import com.lianzai.car.service.CarLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class CarLogServiceImpl implements CarLogService {
	@Autowired
	private CarLogDao carLogDao;

	public CarLogDO get(Integer id)
	{
		return carLogDao.get(id);
	}

	public CarLogDO getByOrderId(String orderId)
	{
		return carLogDao.getByOrderId(orderId);
	}

	public CarLogDO getByCameraId(Long cameraId)
	{
		return carLogDao.getByCameraId(cameraId);
	}

	public CarLogDO getByCarNumberAndCommunity(String carNumber, Long communityId)
	{
		return carLogDao.getByCarNumberAndCommunity(carNumber, communityId);
	}

	public List<CarLogDO> list(Map<String,Object> map)
	{
		return carLogDao.list(map);
	}

	public int count(Map<String,Object> map)
	{
		return carLogDao.count(map);
	}

	public int save(CarLogDO carLog)
	{
		return carLogDao.save(carLog);
	}

	public int syncLog(CarLogDO carLog)
	{
		return carLogDao.syncLog(carLog);
	}

	public int updatePayOrder(CarLogDO carLog)
	{
		return carLogDao.updatePayOrder(carLog);
	}

	public int updatePayStatus(CarLogDO carLog)
	{
		return carLogDao.updatePayStatus(carLog);
	}

	public int payedByCarNumber(CarLogDO carLog)
	{
		return carLogDao.payedByCarNumber(carLog);
	}

	public CarParkInfoDO getParkInfoByCameraId(Long cameraId)
	{
		CarParkInfoDO parkInfo = null;

		//获取对应相机正在等待支付的记录
		CarLogDO exitLog = carLogDao.getByCameraId(cameraId);

		//如果查不出待支付的停车记录，则返回null
		if (exitLog == null)
		{
			return null;
		}

		CarLogDO enterLog = carLogDao.getByCarNumberAndCommunity(exitLog.getCarNumber(), exitLog.getCommunityId());

		//计算停车时长
		long duration = exitLog.getLogTime().getTime() - enterLog.getLogTime().getTime();
		duration /= 1000;  //将毫秒值转换为秒

		parkInfo = new CarParkInfoDO();
		parkInfo.setCarId(exitLog.getCarId());
		parkInfo.setCarNumber(exitLog.getCarNumber());
		parkInfo.setCommunityId(exitLog.getCommunityId());
		parkInfo.setCommunityName(exitLog.getCommunityName());
		parkInfo.setDuration(duration);
		parkInfo.setStartTime(enterLog.getLogTime());
		parkInfo.setEndTime(exitLog.getLogTime());
		parkInfo.setFee(exitLog.getFee());

		return parkInfo;
	}
}
